package com.xuecheng.content.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * Created on 2021/04/02/19:57 周五
 *
 * @author Lantin
 */
@Configuration
public class RestTemplateConfig {

    /**
     * <p>
     *     创建RestTemplate到spring
     * </p>
     *
     * @param factory http request工厂
     * @return RestTemplate
     */
    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {

        return new RestTemplate(factory);
    }

    /**
     * 创建 Http 连接工厂，配置连接时长和读取时长
     * @return ClientHttpRequestFactory
     */
    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(15000);
        factory.setReadTimeout(5000);
        return factory;
    }

}
